package com.djmw.mouse.web.member;

import java.util.Date;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.validation.Validator;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.djmw.mouse.entity.Bookmark;
import com.djmw.mouse.entity.BookmarkId;
import com.djmw.mouse.entity.Company;
import com.djmw.mouse.entity.MemberUser;
import com.djmw.mouse.entity.Position;
import com.djmw.mouse.service.BookmarkService;
import com.djmw.mouse.service.CompanyService;
import com.djmw.mouse.service.PositionService;
import com.djmw.mouse.service.account.ShiroDbRealm.ShiroUser;
import com.google.common.collect.Maps;

/**
 * 收藏管理的Controller, 使用Restful风格的Urls:
 * 
 * 列表: GET /member/bookmark <br>
 * 收藏职位 : POST /member/bookmark/save/position <br>
 * 收藏公司：POST /member/bookmark/save/company <br>
 * 
 * @author michael
 */

@Controller
@RequestMapping(value = "/member/bookmark")
public class BookmarkController {

    private static Map<String, String> sortTypes = Maps.newLinkedHashMap();
    static {
        sortTypes.put("auto", "收藏日期");
    }

    @Autowired
    private BookmarkService bookmarkService;
    @Autowired
    private PositionService positionService;
    @Autowired
    private CompanyService companyService;
    // @Autowired
    // private ApplicationService applicationService;
    // @Autowired
    // private MemberAccountService memberAccountService;
    @Autowired
    private Validator validator;

    @RequestMapping(value = "")
    public String list(Model model, @RequestParam(value = "type", defaultValue = "0") int type,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "result", defaultValue = "all") String result, ServletRequest request) {
        MemberUser user = getCurrentUser();
        Page<Bookmark> bookmarks = null;
        switch (type) {
        case BookmarkId.BOOKMARK_TYPE_ALL:
            bookmarks = bookmarkService.getAllBookmarkByMemberUserId(user.getId(), page, 20);
            break;
        case BookmarkId.BOOKMARK_TYPE_POSITION:
            bookmarks = bookmarkService.getPositionBookmarkByMemberUserId(user.getId(), page, 20);
            break;
        case BookmarkId.BOOKMARK_TYPE_COMPANY:
            bookmarks = bookmarkService.getCompanyBookmarkByMemberUserId(user.getId(), page, 20);
            break;
        }
        model.addAttribute("bookmarks", bookmarks);
        model.addAttribute("sortType", "auto");
        model.addAttribute("sortTypes", sortTypes);
        // Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
        if ("p".equals(result)) {
            return "member/bookmark-position-list";
        } else if ("c".equals(result)) {
            return "member/bookmark-company-list";
        } else {
            return "member/bookmark-list";
        }
    }

    @RequestMapping(value = "save/position", method = RequestMethod.POST)
    public String addPosition(@RequestParam("pid") Long positionId, RedirectAttributes redirectAttributes) {
        MemberUser user = getCurrentUser();
        Position position = positionService.getPosition(positionId);
        if (position == null) {
            redirectAttributes.addFlashAttribute("message", "未知的职位");
            return "redirect:/jobsearch/position/" + positionId;
        }
        Bookmark bookmark = new Bookmark();
        bookmark.setPosition(position);
        bookmark.setMemberUser(user);
        bookmark.setType(BookmarkId.BOOKMARK_TYPE_POSITION);
        bookmark.setCreateDate(new Date());
        Bookmark bm2 = bookmarkService.findExistingPositionBookmark(position, user);
        if (bm2 != null) {
            redirectAttributes.addFlashAttribute("message", "已经收藏过此职位");
            return "redirect:/jobsearch/position/" + positionId;
        }
        position.getBookmark().add(bookmark);
        positionService.savePosition(position, null);
        redirectAttributes.addFlashAttribute("message", "收藏成功");
        return "redirect:/jobsearch/position/" + positionId;
    }

    @RequestMapping(value = "save/company", method = RequestMethod.POST)
    public String addCompany(@RequestParam("cid") Long companyId, RedirectAttributes redirectAttributes) {
        MemberUser user = getCurrentUser();
        Company company = companyService.getCompany(companyId);
        if (company == null) {
            redirectAttributes.addFlashAttribute("message", "未知的公司");
            return "redirect:/jobsearch/company/" + companyId;
        }
        Bookmark bookmark = new Bookmark();
        bookmark.setCompany(company);
        bookmark.setMemberUser(user);
        bookmark.setType(BookmarkId.BOOKMARK_TYPE_COMPANY);
        bookmark.setCreateDate(new Date());
        Bookmark bm2 = bookmarkService.findExistingCompanyBookmark(company, user);
        if (bm2 != null) {
            redirectAttributes.addFlashAttribute("message", "已经收藏过此公司");
            return "redirect:/jobsearch/company/" + companyId;
        }
        company.getBookmark().add(bookmark);
        companyService.saveCompany(company);
        redirectAttributes.addFlashAttribute("message", "收藏成功");
        return "redirect:/jobsearch/company/" + companyId;
    }

    public MemberUser getCurrentUser() {
        ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
        MemberUser memberUser = (MemberUser) user.getUser();
        return memberUser;
    }
}
